import java.awt.Color;
import java.awt.geom.Line2D;
import java.awt.geom.Path2D;
import java.awt.geom.Point2D;
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;

/**
 * 
 * @author Adam and Terry
 *
 */

public class State {
	
	private static final long serialVersionUID = 741713763308816772L;
	private String name;
	private String delimiter;
	private Color colour;
	private  Path2D.Double drawPath;
    private double minLat = 999, maxLat = -999;
    private double minLon = 999, maxLon = -999;
	private ArrayList<Point2D.Double> intersections;
	
	/**
	 * 
	 * @param name
	 * @param delimiter
	 */
	public State(String name, String delimiter) {
		this.name = name;
		this.delimiter =delimiter; 
		this.colour= Color.BLACK;
		this.drawPath = new Path2D.Double();
	}
	
	public Path2D.Double getDrawPath()
	{
		return this.drawPath;
	}
	
	
	
	 public double getMaxLat() {
	        return maxLat;
	    }
	    
	    public double getMaxLon() {
	        return maxLon;
	    }
	    
	    public double getMinLat() {
	        return minLat;
	    }
	    
	    public double getMinLon() {
	        return minLon;
	    }

	
	/**
	 * 
	 * @return get state name 
	 */

	public String getName() {
		return name;
	}
	
	/**
	 * 
	 * @return get state colour 
	 */
	
	public Color getColour(){
		return this.colour;
	}
	
	/**
	 * Reads AGM data. Stops when line "END" is read.
	 * 
	 * @param input
	 *            open file which has just read the number
	 * @throws IoException
	 *             or subclass if something goes wrong
	 */
	
	

	public void read(BufferedReader input, String line) throws IOException {
		//String line = input.readLine();
		Point2D.Double current = null;
		Point2D.Double prev = null;
		Point2D.Double first = null;		

		// Reads all the coords from the file in order and adds the lines to the shape
		while (!line.equalsIgnoreCase("end")) {
			
			String[] parts = line.split(this.delimiter);
			name = parts[0];
					
			if(parts.length > 1){
			double x = java.lang.Double.parseDouble(parts[1]);
			double y = java.lang.Double.parseDouble(parts[2]);
			
			current = new Point2D.Double(x, y);
			
			if (prev != null) {			
				drawPath.append(new Line2D.Double(current.x, current.y, prev.x, prev.y), true);
			}
			else {
				first = current;
			}
			
			if (x < minLon)
                minLon = x;
            else if (x > maxLon)
                maxLon = x;
            if (y < minLat)
                minLat = y;
            else if (y > maxLat)
                maxLat = y;
			
			prev = current;
			line = input.readLine();
			
			
			
			
			
			
			}
		}
		
		// Join the first and last points of the suburb	
		//drawPath.append(new Line2D.Double(current, first), true);
	}
}
